package com.prompt;

import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import com.prompt.SensorHandle.AcceHandler;
import com.prompt.utils.CommonUtil;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service {

	private static final String TAG = "MainService";
	// 1、取得重力感应器Sensor对象
	// 在 Activity 中定义以下成员变量：
	// private SensorManager mManager = null;
	private SensorManagerSimulator mManager;// 使用SensorSimulator模拟时声明SensorManager对象引用的方法
	private Sensor mSensor = null;

	private AcceHandler acceHandle = null;
	
	private AcceHandler getAcceHandle() {
		if(acceHandle == null){
			acceHandle = new AcceHandler(this);
		}
		return acceHandle;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		Toast.makeText(this, "MainService Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");

		
		float fv=Float.valueOf(android.os.Build.VERSION.RELEASE.substring(0,3).trim());
		if(fv >= 2.3)
		{
			android.os.StrictMode.setThreadPolicy(new android.os.StrictMode.ThreadPolicy.Builder()
			 .detectDiskReads()
			 .detectDiskWrites()
			 .detectNetwork()//or .detectAll() for all detectable problems
			 .penaltyLog()
			 .build());
			android.os.StrictMode.setVmPolicy(new android.os.StrictMode.VmPolicy.Builder()
			 .detectLeakedSqlLiteObjects() //探测SQLite数据库操作
			 .penaltyLog() //打印logcat
			 .penaltyDeath()
			 .build());
		}
		

		// mManager= (SensorManager)getSystemService(SENSOR_SERVICE);
		mManager = SensorManagerSimulator.getSystemService(this,
				Context.SENSOR_SERVICE);

//		ConnectionToSensorSimulator conn = new ConnectionToSensorSimulator();
//		conn.execute();

		 mManager.connectSimulator();
		
	}


//	private class ConnectionToSensorSimulator extends AsyncTask<Void, Void, Boolean> {
//
//		@Override
//		protected Boolean doInBackground(Void... params) {
//			Log.d("SENSOR", "CONNECTING TO SENSOR SIMULATOR");
//			mManager.connectSimulator();
//			return true;
//		}
//
//		@Override
//		protected void onPostExecute(Boolean result) {
//			super.onPostExecute(result);
//			if (result) {
//				Log.d("SENSOR", "CONNECTED TO SENSOR SIMULATOR");
//			} else {
//				Log.d("SENSOR", "NOT CONNECTED TO SENSOR SIMULATOR");
//			}
//		}
//
//	};

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		checkRegister();
		return super.onStartCommand(intent, flags, startId);
	}

	private void checkRegister(){
		SharedPreferences listSet = getSharedPreferences("listSet", 0); 
        String itemState;
		for(int i=0;i<5;i++){
        	itemState = listSet.getString("item"+i, null);  
        	if(itemState != null && !(itemState.equals("0"))){
        		if(i == Const.POS_1){
        			mManager.registerListener(myAccelerometerListener,mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        		}else if(i == Const.POS_2){
        			
        		}
        	}
        }
	}
	
	private SensorEventListener myAccelerometerListener = new SensorEventListener() {

		// 复写onSensorChanged方法
		public void onSensorChanged(SensorEvent sensorEvent) {
			Log.i(TAG, "onSensorChanged");
			if (sensorEvent.sensor.TYPE_ACCELEROMETER == Sensor.TYPE_ACCELEROMETER) {
				Log.i(TAG, "onSensorChanged TYPE_ACCELEROMETER");

				// 图解中已经解释三个值的含义
				float xLateral = sensorEvent.values[0];
				float yLongitudinal = sensorEvent.values[1];
				float zVertical = sensorEvent.values[2];
				Boolean result = getAcceHandle().proccess(xLateral, yLongitudinal, zVertical);
				if(result){
					CommonUtil.getInstance().lightSc();
					Log.i(TAG, "alarm start");
					Intent intent = new Intent(MainService.this, AlarmActivity.class);  
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent); 
					GlobalState.getInstance().setIsALarm(true);
				}
			}
		}

		// 复写onAccuracyChanged方法
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			Log.i(TAG, "onAccuracyChanged");
		}
	};

	@Override
	public void onDestroy() {
		Toast.makeText(this, "MainService Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
		mManager.unregisterListener(myAccelerometerListener);
		// player.stop();
	}

	@Override
	public void onStart(Intent intent, int startid) {
		// Toast.makeText(this, "MainService Started",
		// Toast.LENGTH_LONG).show();

		Log.d(TAG, "onStart");
		// player.start();
	}
}
